﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using ZDSoft.Student.Domain;

namespace ZDSoft.Student.Manager
{
    public class StudentInfoManager
    {
        string SqlConnection = "Data Source=TX-PC\\MSSQLSERVER1;Initial Catalog=Student;Integrated Security=True";
        DataTable dt = null;
        DataSet ds = null;
        SqlDataReader sdr = null;//流
        SqlDataAdapter sda = null;//填充

        public IList<StudentInfo> GetAll()
        {
            using (SqlConnection con = new SqlConnection(SqlConnection))
            {
                if (con.State == ConnectionState.Closed)//判断数据库连接是否打开
                    con.Open();//打开数据库连接
                SqlCommand cmd = new SqlCommand("select * from StudentInfo");
                cmd.Connection = con;
                sda = new SqlDataAdapter(cmd);
                dt = new DataTable();
                sda.Fill(dt);
                con.Close();
            }
            IList<StudentInfo> StudentList = new List<StudentInfo>();
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow item in dt.Rows)
                    {
                        StudentInfo stu = new StudentInfo();
                        stu.Id = int.Parse(item["Id"].ToString());
                        stu.StudentNumber = item["StudentNumber"].ToString();
                        stu.StudentName = item["StudentName"].ToString();
                        stu.Sex = int.Parse(item["Sex"].ToString());
                        stu.Age = int.Parse(item["Age"].ToString());
                        StudentList.Add(stu);
                    }
                }
            }
            return StudentList;
        }
    }
}
